function getUrlEncodedKey(key) {
    hash = window.location.hash;
    var re = new RegExp("[#|&]" + key + "=(.*?)&");
    var matches = re.exec(hash + "&");
    if (!matches || matches.length < 2)
        return "";
    return decodeURIComponent(matches[1].replace("+", " "));
};

function setUrlEncodedKey(key, value) {

    hash = window.location.hash;
    var q = hash + "&";
    var re = new RegExp("[#|&]" + key + "=.*?&");
    if (!re.test(q))
        q += key + "=" + encodeURI(value);
    else
        q = q.replace(re, "&" + key + "=" + encodeURIComponent(value) + "&");
    q = q.trimStart("&").trimEnd("&");
    window.location.hash = q;
    return q;
};

String.prototype.trimEnd = function(c) {
    if (c)
        return this.replace(new RegExp(c.escapeRegExp() + "*$"), '');
    return this.replace(/\s+$/, '');
};

String.prototype.trimStart = function(c) {
    if (c)
        return this.replace(new RegExp("^" + c.escapeRegExp() + "*"), '');
    return this.replace(/^\s+/, '');
};

String.prototype.escapeRegExp = function() {
    return this.replace(/[.*+?^${}()|[\]\/\\]/g, "\\$0");
};

function findValue(li) {
        if( li == null ) return alert("No match!");
        // if coming from an AJAX call, let's use the HerbId as the value
        if( !!li.extra ) var sValue = li.extra[0];
        // otherwise, let's just display the value in the text box
        else var sValue = li.selectValue;
}

function selectItem(li) {
        findValue(li);
}

function formatItem(row) {
        return row[0] + " (id: " + row[1] + ")";
}

function lookupAjax(){
        var oSuggest = $("#HerbAjax")[0].autocompleter;
        oSuggest.findValue();
        return false;
}

function lookupLocalHerbs(){
	    setUrlEncodedKey('q', 'herbquery');
        setUrlEncodedKey('a', $('#HerbLocal').val());
        setUrlEncodedKey('t', 'herb');
        doSearch('herbquery', $('#HerbLocal').val(), searchSuccess);
        return false;
}

function lookupLocalSymptoms(){
	    setUrlEncodedKey('q', 'symptomquery');
        setUrlEncodedKey('a', $('#SymptomsLocal').val());
        setUrlEncodedKey('t', 'symptom');
        doSearch('symptomquery', $('#SymptomsLocal').val(), searchSuccess);
        return false;
}

function clearCanvas() {
  $('#canvas').html('');
  window.location.hash='';
}

function searchSuccess(data, textStatus, jqXHR) {
  $('#canvas').html(data.reply);
}

function doSearch(type, args, callback) {
  var data = new Object();
  data.lang = (document.URL.indexOf('/nl/')==-1) ? 'en' : 'nl';
  data.type = type;
  data.args = args;
  var url = '/' + lang + '/ajax';
  jQuery.post(url, data, callback, 'json');
}

window.onload=function() {
	
$('#isherbselected').click(function() {
	if(this.checked === false) {
		this.checked = true;
		return '';
	}
    clearCanvas();
    $('#issymptomselected')[0].checked=!this.checked;
    $('#isalphabetselected')[0].checked=!this.checked;
    $('#herbsearch').toggle(this.checked);
    $('#symptomsearch').toggle(!this.checked);
    $('#HerbLocal').focus();    
    $('#alpha').toggle(!this.checked);
});

$('#issymptomselected').click(function() {
	if(this.checked === false) {
		this.checked = true;
		return '';
	}
    clearCanvas();
    $('#isherbselected')[0].checked=!this.checked;
    $('#isalphabetselected')[0].checked=!this.checked;
    $('#herbsearch').toggle(!this.checked);
    $('#symptomsearch').toggle(this.checked);
    $('#SymptomsLocal').focus();
    $('#alpha').toggle(!this.checked);
});

$('#isalphabetselected').click(function() {
	if(this.checked === false) {
		this.checked = true;
		return '';
	}
    clearCanvas();
    $('#isherbselected')[0].checked=!this.checked;
    $('#issymptomselected')[0].checked=!this.checked;
    $('#herbsearch').toggle(!this.checked);
    $('#symptomsearch').toggle(!this.checked);
    $('#alpha').toggle(this.checked);
});

doSearch('herbs', '', function(data) {
        $("#HerbLocal").autocompleteArray(
                data.reply,
                {
                        delay:10,
                        minChars:1,
                        matchSubset:1,
                        onItemSelect:selectItem,
                        onFindValue:findValue,
                        autoFill:true,
                        maxItemsToShow:10
                }
        );
});

doSearch('symptoms', '', function(data) {
        $("#SymptomsLocal").autocompleteArray(
                data.reply,
                {
                        delay:10,
                        minChars:1,
                        matchSubset:1,
                        onItemSelect:selectItem,
                        onFindValue:findValue,
                        autoFill:true,
                        maxItemsToShow:10
                }
        );
});

doSearch('links', '', function(data) {
        $("#herblist").html(data.reply);
        $('.herb_link').click(function() {
            clearCanvas();
            setUrlEncodedKey('q', 'herbquery');
            setUrlEncodedKey('a', this.name);
            setUrlEncodedKey('t', 'alphabet');
            doSearch('herbquery', this.name, searchSuccess);
        });
});

var searchType = getUrlEncodedKey('q');
var args = getUrlEncodedKey('a');
if(searchType && args) {	
	var type = getUrlEncodedKey('t');
	if(type) {
		$('#isalphabetselected')[0].checked=false;
    	$('#is' + type + 'selected')[0].checked=true;
    }
	setUrlEncodedKey('q', searchType);
    setUrlEncodedKey('a', args);
    setUrlEncodedKey('t', type);
	doSearch(searchType, args, searchSuccess);
}

}
